#include "lastproject.h"
#include "ui_lastproject.h"

LastProject::LastProject(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::LastProject)
{
    ui->setupUi(this);
}

LastProject::~LastProject()
{
    delete ui;
}

void LastProject::getVal(){
    double Xval = 0;
    Yval[0]=  1;
    int n = 10;
    double h = 0.1;
    this->ui->plainTextEdit->appendPlainText(QString::number(Yval[0]));
    for(int i=0;i<n;i++){
        Yval[i+1] = Yval[i] + h/2*(fun(Xval+h*i,Yval[i])+fun(Xval+h*(i+1),Yval[i]+h*fun(Xval+i*h,Yval[i])));
        this->ui->plainTextEdit->appendPlainText(QString::number(Yval[i+1]));
    }

}

double LastProject::fun(double x,double y){
 //   return -x*y*y;
    return y-2*x/y;
}


void LastProject::getVal_Run(){
    double Xval = 0;
    double h = 0.1;
    int n = 10;

    Yval[0] = 1;
    this->ui->plainTextEdit_2->appendPlainText(QString::number(Yval[0]));
    double K1,K2,K3,K4;
    for(int i=0;i<n;i++){
        K1 = fun(Xval+i*h,Yval[i]);
        K2 = fun(Xval+i*h+h/2,Yval[i]+h*K1/2);
        K3 = fun(Xval+i*h+h/2,Yval[i]+h*K2/2);
        K4 = fun(Xval+i*h+h,Yval[i]+h*K3);
        Yval[i+1] = Yval[i] + h*(K1+K2+K3+K4)/6;
        this->ui->plainTextEdit_2->appendPlainText(QString::number(Yval[i+1]));
    }

}

void LastProject::on_pushButton_clicked()
{
    getVal();
    getVal_Run();
}
